﻿using Microsoft.SharePoint;

namespace SharePoint.Orm.SiteBuilders {
	/// <summary>
	/// Base class for all site builders.
	/// </summary>
	/// <typeparam name="T">The type of object being built (e.g. SPContentType).</typeparam>
	public abstract class BuilderBase<T> {
		/// <summary>
		/// Required.  This is static so you can set it once and instantiate per content type.  Don't forget to set it back to null
		/// since it is static and that may make disposing the site more difficult.
		/// </summary>
		public static SPWeb Site { get; set; }

		/// <summary>
		/// Finds T if it exists and possibly does some syncing
		/// </summary>
		/// <returns></returns>
		public abstract T FindAdd();

		/// <summary>
		/// Use this periodically to avoid the dreaded SPException when you try to create more than about 10 lists: Save Conflict.  
		/// Your changes conflict with those made concurrently by another user. If you want your changes to be applied, click 
		/// Back in your Web browser, refresh the page, and resubmit your changes.
		///	It seems heavy haned, but it seems to work, see: 
		/// http://www.theartofsharepoint.com/2007/05/microsoftsharepointspexception-save.html
		/// </summary>
		public static void RefreshSite() {
			SPWeb newSite = Site.Site.OpenWeb(Site.ID);
			Site.Dispose();
			Site = newSite;
		}
	}
}
